from django.conf                import settings
from django.contrib.auth.models import User
from django.utils.importlib     import import_module

_superuser = None

def get_superuser():
    global _superuser
    if not _superuser:
        user_list = User.objects.filter(is_superuser=True,is_active=True).order_by('id')
        if len(user_list):
            _superuser = user_list[0]
            return _superuser
        raise User.DoesNotExist,"No superuser defined"
    return _superuser

def iterappmodules(submodule=None):
    for module_name in settings.INSTALLED_APPS:
        app_label = module_name.split('.')[-1]
        if submodule:
            module_name = ".".join((module_name,submodule))
        try:
            yield app_label,import_module(module_name)
        except ImportError:
            continue

